package com.qiangesoft.blog.file.core.minio;

import com.qiangesoft.blog.file.model.FileInfoVo;
import io.minio.StatObjectResponse;
import io.minio.messages.Bucket;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;
import java.util.Optional;

/**
 * minio文件服务
 *
 * @author qiange
 * @date 2023-04-21
 */
public interface MinioTemplate {

    /**
     * 超过1MB后进行压缩
     */
    long maxSize = 1024 * 1024;

    /**
     * 创建bucket
     */
    void createBucket(String bucketName) throws Exception;

    /**
     * 上传文件（默认桶）
     */
    FileInfoVo uploadFile(InputStream inputStream, String contentType) throws Exception;

    /**
     * 上传文件
     */
    FileInfoVo uploadFile(InputStream inputStream, String contentType, String bucketName) throws Exception;

    /**
     * 上传文件（默认桶）
     */
    FileInfoVo uploadFile(MultipartFile file) throws Exception;

    /**
     * 上传文件
     */
    FileInfoVo uploadFile(MultipartFile file, String bucketName) throws Exception;

    /**
     * 获取全部bucket
     *
     * @return
     */
    List<Bucket> getAllBuckets() throws Exception;

    /**
     * 根据bucketName获取信息
     *
     * @param bucketName bucket名称
     */
    Optional<Bucket> getBucket(String bucketName) throws Exception;

    /**
     * 根据bucketName删除信息
     *
     * @param bucketName bucket名称
     */
    void removeBucket(String bucketName) throws Exception;

    /**
     * 获取⽂件外链
     *
     * @param bucketName bucket名称
     * @param objectName ⽂件名称
     * @param expires    过期时间 <=7
     * @return url
     */
    String getObjectURL(String bucketName, String objectName, Integer expires) throws Exception;

    /**
     * 获取⽂件
     *
     * @param bucketName bucket名称
     * @param objectName ⽂件名称
     * @return ⼆进制流
     */
    InputStream getObject(String bucketName, String objectName) throws Exception;

    /**
     * 上传⽂件
     *
     * @param bucketName bucket名称
     * @param objectName ⽂件名称
     * @param stream     ⽂件流
     * @throws Exception https://docs.minio.io/cn/java-minioClient-api-reference.html#putObject
     */
    void putObject(String bucketName, String objectName, InputStream stream) throws Exception;

    /**
     * 上传⽂件
     *
     * @param bucketName  bucket名称
     * @param objectName  ⽂件名称
     * @param stream      ⽂件流
     * @param size        ⼤⼩
     * @param contextType 类型
     * @throws Exception https://docs.minio.io/cn/java-minioClient-api-reference.html#putObject
     */
    void putObject(String bucketName, String objectName, InputStream stream, long size, String contextType) throws Exception;

    /**
     * 获取⽂件信息
     *
     * @param bucketName bucket名称
     * @param objectName ⽂件名称
     * @throws Exception https://docs.minio.io/cn/java-minioClient-api-reference.html#statObject
     */
    StatObjectResponse getObjectInfo(String bucketName, String objectName) throws Exception;

    /**
     * 删除⽂件
     *
     * @param bucketName bucket名称
     * @param objectName ⽂件名称
     * @throws Exception https://docs.minio.io/cn/java-minioClient-apireference.html#removeObject
     */
    void removeObject(String bucketName, String objectName) throws Exception;

    /**
     * 删除⽂件
     *
     * @param objectName ⽂件名称
     * @throws Exception https://docs.minio.io/cn/java-minioClient-apireference.html#removeObject
     */
    void removeObject(String objectName) throws Exception;

    /**
     * 删除文件
     *
     * @param bucketName
     * @param objectNames
     * @throws Exception
     */
    void removeObjects(String bucketName, List<String> objectNames) throws Exception;

    /**
     * 删除文件
     *
     * @param objectNames
     * @throws Exception
     */
    void removeObjects(List<String> objectNames) throws Exception;

    /***
     * 上传视频（默认桶）
     * @param file
     * @return
     * @throws Exception
     */
    FileInfoVo uploadVideo(MultipartFile file) throws Exception;

    /***
     * 上传视频
     * @param file
     * @param bucketName
     * @return
     * @throws Exception
     */
    FileInfoVo uploadVideo(MultipartFile file, String bucketName) throws Exception;

    /**
     * 下载文件（默认桶）
     *
     * @param filename
     * @param request
     * @param response
     */
    void download(String filename, HttpServletRequest request, HttpServletResponse response) throws Exception;

    /**
     * 下载文件
     *
     * @param bucketName
     * @param filename
     * @param request
     * @param response
     */
    void download(String bucketName, String filename, HttpServletRequest request, HttpServletResponse response) throws Exception;
}
